<?php

namespace app\service;

use AlibabaCloud\SDK\Dypnsapi\V20170525\Dypnsapi;
use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dypnsapi\V20170525\Models\GetMobileRequest;
use AlibabaCloud\SDK\Dypnsapi\V20170525\Models\VerifyMobileRequest;

class AliMobileService
{

    public static ?AliMobileService $_instance = null;

    /**
     * @return AliMobileService|mixed
     */
    public static function instance(): ?AliMobileService
    {
        if (!static::$_instance) static::$_instance = new self();
        return static::$_instance;
    }

    /**
     * 使用AK&SK初始化账号Client
     *
     * @return Dypnsapi Client
     */
    public static function createClient()
    {
        $config = new Config([
            //用户登录名称 sz_app@148.onaliyun.com
            "accessKeyId"     => 'owenweb',
            "accessKeySecret" => 'owenweb',
        ]);
        // 访问的域名
        $config->endpoint = "owenweb.aliyuncs.com";
        return new Dypnsapi($config);
    }

    //认证token的使用有效期
    //电信10分钟，仅可使用一次；联通30分钟，仅可使用一次；移动2分钟，仅可使用一次。
    //接口文档https://help.aliyun.com/document_detail/15286.htm?spm=a2crMIj
    public function GetMobile(string $AccessToken): array
    {
        $client           = self::createClient();
        $getMobileRequest = new GetMobileRequest([]);
        // 复制代码运行请自行打印 API 的返回值
        $getMobileRequest->accessToken = $AccessToken;
        $ret                           = $client->getMobile($getMobileRequest);
        return $ret->body->toMap();
    }

    //接口文档https://help.aliyun.com/document_detail/1287.html
    public function VerifyMobile(string $AccessCode, string $PhoneNumber): array
    {
        $client                           = self::createClient();
        $verifyMobileRequest              = new VerifyMobileRequest([]);
        $verifyMobileRequest->accessCode  = $AccessCode;
        $verifyMobileRequest->phoneNumber = $PhoneNumber;
        $ret                              = $client->verifyMobile($verifyMobileRequest);
        return $ret->body->toMap();
    }
}